<template>
  <div>
    <UploadExcel :on-success="onSuccess"></UploadExcel>
  </div>
</template>

<script>
import { importEmployee } from '@/api/employees'
export default {
  components: {},
  props: [],
  data() {
    return {}
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async  onSuccess({ header, results }) {
      // header = ['手机号', '姓名', '入职日期', '转正日期', '工号']
      // results = [{"手机号":13941130879,"姓名":"高大山","入职日期":43535,"转正日期":43719,"工号":20089}]
      // 目标数据的结构:
      // [{"mobile":13941130879,"username":"高大山","timeOfEntry":43535,"correctionTime":43719,"workNumber":20089}]
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }

      results.forEach(o => {
        for (const key in o) {
          if (key === '入职日期' || key === '转正日期') {
            o[userRelations[key]] = new Date(this.formatDate(o[key], '/'))
          }
          delete o[key]
        }
      })
      // 发请求
      await importEmployee(results)
      // 提示成功
      this.$message.success('操作成功')
      // 返回上一页
      this.$router.back()
      console.log(results)
    },
    formatDate(numb, format) {
      const time = new Date((numb - 1) * 24 * 3600000 + 1)
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return year + format + month + format + date
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
